package com.douqu.game.battle.controller;

import com.douqu.game.battle.entity.BattleMasterAObject;
import com.douqu.game.battle.entity.MonsterBattleTmp;
import com.douqu.game.battle.entity.PlayerBattleTmp;
import com.douqu.game.core.controller.*;
import com.douqu.game.core.entity.Level;
import com.douqu.game.core.entity.StarCheck;
import com.douqu.game.core.entity.battle.SpriteBattleTmp;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.protobuf.SGCommonProto;

/**
* Created by bean on 2017/7/25.
*/
public class PVEController extends BattleController {

    private int levelId;

    public PVEController(String battleId, PlayerController player, SpriteController target, SGCommonProto.E_BATTLE_TYPE battleType,
                         SGCommonProto.Pos leftBottom, SGCommonProto.Pos rightTop)
    {
        super(battleId, player, target, battleType, leftBottom, rightTop);

        if(target instanceof MonsterController)
        {
            this.levelId = ((MonsterController) target).getLevel().id;
        }
    }

    @Override
    public void init()
    {
        super.init();

        //队伍2的主将
        BattleMasterAObject masterAObject2 = new BattleMasterAObject(target, createMasterUniqueId + BattleController.TEAM_2);
        masterAObject2.setTeamNo(BattleController.TEAM_2);
        masterAObject2.setPosition(getMiddlePos(BattleController.TEAM_2));

        SpriteBattleTmp pbt2 = new MonsterBattleTmp();
        pbt2.setBattleParent(this);
        pbt2.setReady(true);
        pbt2.init(target, masterAObject2, BattleController.TEAM_2);
        target.setAttachment(pbt2);

        pbt2.setTargetBattleTmp(player.getAttachment());
        player.getAttachment().setTargetBattleTmp(pbt2);

        battleInfo.setLevelId(levelId);
    }


    @Override
    public int getStar(SpriteBattleTmp spriteBattleTmp)
    {
        if(spriteBattleTmp.getTeamNo() == TEAM_2)
            return 0;

        int result = 0;

        DataFactory dataFactory = DataFactory.getInstance();
        Level level = dataFactory.getGameObject(DataFactory.LEVEL_KEY, levelId);
        if(level != null)
        {
            StarCheck starCheck = null;
            for(int sc : level.starChecks)
            {
                starCheck = dataFactory.getGameObject(DataFactory.STAR_CHECK_KEY, sc);
                if(starCheck.type == ConstantFactory.STAR_TYPE_BATTLE_TIME)
                {
                    int time = battleTime / 1000;
                    if(time >= starCheck.values[0] && time <= starCheck.values[1])
                        result += starCheck.star;
                }
                else if(starCheck.type == ConstantFactory.STAR_TYPE_HP_RATE)
                {
                    int hp = spriteBattleTmp.getMasterSoldier().getHp();
                    int maxHp = spriteBattleTmp.getMasterSoldier().getMaxHp();
                    double temp = (double)hp / maxHp;
                    int rate = (int) (temp * 100);
                    if(rate >= starCheck.values[0] && rate <= starCheck.values[1])
                        result += starCheck.star;
                }
                else if(starCheck.type == ConstantFactory.STAR_TYPE_KILL_COUNT)
                {

                }
            }
        }
        return result + spriteBattleTmp.star;
    }


    @Override
    public void checkStar(SpriteBattleTmp spriteBattleTmp, int race)
    {
        DataFactory dataFactory = DataFactory.getInstance();
        Level level = dataFactory.getGameObject(DataFactory.LEVEL_KEY, levelId);
        if(level != null)
        {
            StarCheck starCheck = null;
            for(int sc : level.starChecks)
            {
                starCheck = dataFactory.getGameObject(DataFactory.STAR_CHECK_KEY, sc);
                if(starCheck.type == ConstantFactory.STAR_TYPE_RACE_ID)
                {
                    if(starCheck.values[0] == race)
                        spriteBattleTmp.addStar(starCheck.star);
                }
            }
        }
    }

}
